/**
 * Copyright 2011 Adrian Witas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.abstractmeta.commandify.core.command.collection;

import org.abstractmeta.commandify.command.Commands;
import org.abstractmeta.commandify.core.command.AbstractConstructorCommand;
import org.abstractmeta.commandify.core.command.CommandifyCollection;

import java.util.*;

/**
 * Represents a sortedSet which implement commandify.
 * This enables all mutable collection method calls being encapsulated as method commands.
 * If collection value implements commandify then as long as it is possible to build cloned version build, it will be used
 * instead of actual value.
 */
public class CommandifySortedSet<T> extends CommandifyCollection<T> implements SortedSet<T> {

    protected final SortedSet<T> sortedSet;

    public CommandifySortedSet(Class type, SortedSet<T> sortedSet) {
        super(type, sortedSet);
        this.sortedSet = sortedSet;
    }


    @SuppressWarnings("unchecked")
    protected void addConstructorCommand() {
        Commands commands = getCommands();
        commands.setConstructorCommand(new AbstractConstructorCommand<SortedSet<T>>() {
            @Override
            public SortedSet<T> execute() {
                return new TreeSet<T>();
            }
        });
    }


    @Override
    public Comparator<? super T> comparator() {
        return sortedSet.comparator();
    }

    @Override
    public SortedSet<T> subSet(T t, T t2) {
        return sortedSet.subSet(t, t2);
    }

    @Override
    public SortedSet<T> headSet(T t) {
        return sortedSet.headSet(t);
    }

    @Override
    public SortedSet<T> tailSet(T t) {
        return sortedSet.tailSet(t);
    }

    @Override
    public T first() {
        return sortedSet.first();
    }

    @Override
    public T last() {
        return sortedSet.last();
    }
}
